<?php
!function_exists('readover') && die('Forbidden');
require_once(D_P.'data/cache/cate.php');

/**
 *  栏目调用的类,用于显示栏目菜单,子栏目,栏目树,以及当前位置
 *
 */
class Cate{
	var $catedb;
	var $position;
	var $cateselect;
	
	/**
	 * 当前用户能够浏览的目录
	 *
	 * @var array
	 */
	protected $allowCid = array();
	
	function __construct(){ //PHP5
		global $catedb;
		$this->catedb = $catedb;
		$this->nav();
	}
	
	function Cate(){ //PHP4
		$this->__construct();
	}
	
	/**
	 * 返回栏目树状
	 *
	 * @return string
	 */
	function tree(){
		$this->cateselect = '';
		$this->getTree($this->catedb);
		return $this->cateselect;
	}
	
	/**
	 * 显示栏目菜单，如果有参数cid，则显示其子栏目
	 *
	 * @param integer $cid
	 * @return array
	 */
	function menu($cid=0){
		global $very, $login;
		$menu = array();
		foreach ($this->catedb as $key=>$c)
		{
			if($c['type']!=1) continue; //跳过不显示的栏目
			if (!$this->checkPriv($c)) continue;
			$c['url'] = $c['listurl'];
			$c['up']==$cid && $menu[$key]=$c;
		}
		return $menu;
	}
	
	/**
	 * 返回当前位置
	 *
	 */
	function nav(){
		global $view,$cid,$very;
		if($cid){
			$this->findFather($cid);
		}
		if($view['title']){
			$this->position = $this->position." » ".$view['title'];
		}
		$this->position = "<a href=\"$very[url]\">首页</a>". $this->position;
	}
	
	function getTree($catedb, $cid='')
	{
		foreach ($catedb as $cate){
			if($cate['mid']==0 || $cate['type']==0) continue;
			
			if($cid=='' && $cate['up']!=0)
			{
				continue;
			}
			elseif($cid && $cate['up']!=$cid)
			{
				continue;
			}
			elseif (!$this->checkPriv($cate))
			{
				continue;
			}
			$add = '';
			if($cate['depth']==1){
				$add = "&raquo;";
			}else{
				$add = '|-';
				$repeatnum = ($cate['depth']-1);
				$add.= str_repeat('--',$repeatnum);		
			}
			if ($cate['mid']==0) {
				$disabled = "disabled=\"disabled\"";
			}else{
				$disabled = '';
			}
			$this->allowCid[] = $cate['cid']; //加入到允许浏览
			$this->cateselect.="<option value=\"$cate[cid]\" $disabled>$add$cate[cname]</option>";
			// unset($catedb[$cid]);
			if(count($catedb)==0){
				return ;
			}
			$this->getTree($catedb,$cate['cid']);
		}
	}
	
	/**
	 * 根据当前cid不断向上获取其父ID
	 *
	 * @param integer $cid
	 */
	function findFather($cid){
		$this->position = " » <a href=".$this->catedb[$cid]['listurl'].">".$this->catedb[$cid]['cname']."</a> " . $this->position;
		$up = $this->catedb[$cid]['up'];
		if($up){
			$this->findFather($up);
		}
	}
	
	/**
	 * 检验当前用户是否有浏览此目录的权限
	 *
	 * @param mixed $cate
	 * @return boolean
	 */
	protected function checkPriv($cate)
	{
		if (function_exists('adminmsg')) return true;
		if (is_int($cate)) //说明传进来的是一个数字
		{
			$cateinfo = $this->catedb[$cate];
		}
		else 
		{
			$cateinfo = $cate;
		}
		
		global $login;
		if ($login['ifadmin']) return true; //管理员可以查看所有目录
		
		if (empty($login) && !$cateinfo['guestview']) return false; //无游客访问权限
		if (!empty($cateinfo['groupids']))
		{
			if (empty($login)) return false; //这个时候普通游客也不会有访问权限
			if (!in_array($login['groupid'], explode(',', $cateinfo['groupids']))) return false;
		}
		return true;
	}
	
	/**
	 * 返回允许浏览的目录
	 *
	 * @return array
	 */
	public function getAllow()
	{
		return $this->allowCid;
	}
}
?>